﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin : System.Web.UI.Page
{
    ManageDatabase data;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            if (data == null)
            {
                data = new ManageDatabase();
            }
            if (User.IsInRole("admin"))
            {
                if (!IsPostBack)
                {
                    Initialize();
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
        
    }

    private void Initialize()
    {

        AdminTypeDropDownList.Items.Add(new ListItem("--Összes--", "0"));
        AdminTypeDropDownList.Items.Add(new ListItem("Sorozatok", "1"));
        AdminTypeDropDownList.Items.Add(new ListItem("Évadok", "2"));
        AdminTypeDropDownList.Items.Add(new ListItem("Epizódok", "3"));

        AdminStatusDropDownList.Items.Add(new ListItem("--Összes--", ""));
        AdminStatusDropDownList.Items.Add(new ListItem("Engedélyezett tartalmak", "1"));
        AdminStatusDropDownList.Items.Add(new ListItem("Letiltott tartalmak", "-1"));
        AdminStatusDropDownList.Items.Add(new ListItem("Új tartalmak", "0"));

        GetContent(0,"");
    }

    protected void AdminList_Click(object sender, EventArgs e) {
        GetContent(Convert.ToInt32(AdminTypeDropDownList.SelectedValue), AdminStatusDropDownList.SelectedValue);
    }

    private void GetContent(int type, string status) {
        data = new ManageDatabase();
        bool series = true;
        bool seasons = true; 
        bool episodes = true;
        bool statusFlag = false;
        if(type == 1){
            seasons = false; 
            episodes = false;
        }
        else if(type == 2){
            series = false; 
            episodes = false;
        }
        else if(type == 3){
            seasons = false; 
            series = false;
        }
        int statusInt = 0;
        if(status != ""){
            statusFlag = true;
            statusInt = Convert.ToInt32(status);
        }

        List<Content> result = data.SearchAdminList(series, seasons, episodes, statusInt, statusFlag);

        Label ResultText = new Label();
        Table ResultTable = new Table();
        ResultTable.CssClass = "gradient-style";

        if (result != null)
        {
            TableHeaderRow ResultHeaderRow = new TableHeaderRow();
            TableHeaderCell ResultHeaderCell1 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell2 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell3 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell4 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell5 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell6 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell7 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell8 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell9 = new TableHeaderCell();
            Label Cell1 = new Label();
            Label Cell2 = new Label();
            Label Cell3 = new Label();
            Label Cell4 = new Label();
            Label Cell5 = new Label();
            Label Cell6 = new Label();
            Label Cell7 = new Label();
            Label Cell8 = new Label();
            Label Cell9 = new Label();
            Cell1.Text = "#";
            Cell2.Text = "Sorozat";
            Cell3.Text = "Évad";
            Cell4.Text = "Epizód";
            Cell5.Text = "Típus";
            Cell6.Text = "Státusz";
            Cell7.Text = "Létrehozta";
            Cell8.Text = "Létrehozás dátuma";
            Cell9.Text = "#";
            ResultHeaderCell1.Controls.Add(Cell1);
            ResultHeaderCell2.Controls.Add(Cell2);
            ResultHeaderCell3.Controls.Add(Cell3);
            ResultHeaderCell4.Controls.Add(Cell4);
            ResultHeaderCell5.Controls.Add(Cell5);
            ResultHeaderCell6.Controls.Add(Cell6);
            ResultHeaderCell7.Controls.Add(Cell7);
            ResultHeaderCell8.Controls.Add(Cell8);
            ResultHeaderCell9.Controls.Add(Cell9);
            ResultHeaderRow.Controls.Add(ResultHeaderCell1);
            ResultHeaderRow.Controls.Add(ResultHeaderCell2);
            ResultHeaderRow.Controls.Add(ResultHeaderCell3);
            ResultHeaderRow.Controls.Add(ResultHeaderCell4);
            ResultHeaderRow.Controls.Add(ResultHeaderCell5);
            ResultHeaderRow.Controls.Add(ResultHeaderCell6);
            ResultHeaderRow.Controls.Add(ResultHeaderCell7);
            ResultHeaderRow.Controls.Add(ResultHeaderCell8);
            ResultHeaderRow.Controls.Add(ResultHeaderCell9);
            ResultTable.Controls.Add(ResultHeaderRow);

            int i,j;
            Content temp;
            for (i = (result.Count - 1); i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    if (result[j - 1].createdDate < result[j].createdDate)
                    {
                        temp = result[j - 1];
                        result[j - 1] = result[j];
                        result[j] = temp;
                    }
                }
            }
            i = 0;
            foreach (Content item in result)
            {
                TableRow InfoRow = new TableRow();
                TableCell Info1Cell = new TableCell();
                TableCell Info2Cell = new TableCell();
                TableCell Info3Cell = new TableCell();
                TableCell Info4Cell = new TableCell();
                TableCell Info5Cell = new TableCell();
                TableCell Info6Cell = new TableCell();
                TableCell Info7Cell = new TableCell();
                TableCell Info8Cell = new TableCell();
                TableCell Info9Cell = new TableCell();

                Label Count = new Label();
                Label SeriesTitle = new Label();
                Label Season = new Label();
                Label Episode = new Label();
                Label Type = new Label();
                Label Status = new Label();
                Label User = new Label();
                Label Date = new Label();
                HyperLink Navigate = new HyperLink();

                i++;
                Count.Text = i.ToString() + ". ";

                SeriesTitle.Text = item.seriesTitle;
                if (item.season != 0)
                    Season.Text = item.season.ToString() + ". ";
                else
                    Season.Text = " -";
                if (item.episode != 0)
                    Episode.Text = item.episode.ToString() + ". ";
                else
                    Episode.Text = " -";

                Type.Text = item.type;
                if (item.status == 1)
                    Status.Text = "Engedélyezett";
                else if (item.status == -1)
                    Status.Text = "Letiltott";
                else if (item.status == 0)
                    Status.Text = "Új";

                User.Text = data.GetUserById(Convert.ToInt32(item.userId));
                Date.Text = String.Format("{0:yyyy-MM-dd HH:mm:ss}", item.createdDate);

                if (item.type.Equals("sorozat"))
                    Navigate.NavigateUrl = "Seasons.aspx?sorozat=" + item.id.ToString();
                else if (item.type.Equals("evad"))
                    Navigate.NavigateUrl = "Episodes.aspx?evad=" + item.id.ToString();
                else if (item.type.Equals("epizod"))
                    Navigate.NavigateUrl = "ViewEpisode.aspx?epizod=" + item.id.ToString();

                Navigate.Text = "Link";
                Info1Cell.Controls.Add(Count);
                Info2Cell.Controls.Add(SeriesTitle);
                Info3Cell.Controls.Add(Season);
                Info4Cell.Controls.Add(Episode);
                Info5Cell.Controls.Add(Type);
                Info6Cell.Controls.Add(Status);
                Info7Cell.Controls.Add(User);
                Info8Cell.Controls.Add(Date);
                Info9Cell.Controls.Add(Navigate);

                InfoRow.Controls.Add(Info1Cell);
                InfoRow.Controls.Add(Info2Cell);
                InfoRow.Controls.Add(Info3Cell);
                InfoRow.Controls.Add(Info4Cell);
                InfoRow.Controls.Add(Info5Cell);
                InfoRow.Controls.Add(Info6Cell);
                InfoRow.Controls.Add(Info7Cell);
                InfoRow.Controls.Add(Info8Cell);
                InfoRow.Controls.Add(Info9Cell);

                ResultTable.Controls.Add(InfoRow);
            }
        }

        AdminResultPanel.Controls.Add(ResultTable);
    }
}